package com.example2016.cjj.mycustomviewdemo.MyView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.example2016.cjj.mycustomviewdemo.R;

import java.util.ArrayList;

/**
 * Created by CJJ on 2017/3/10.
 */

public class CustomImageGroup extends ViewGroup {

    private static final String TAG = "CustomImageGroup";

    public static final int MAX_PHOTO_NUMBER =6;//最大的图片数

    private int[] constImageIds ={R.mipmap.meinu,R.mipmap.meinv2,R.mipmap.meinv3,
            R.mipmap.meinv4,R.mipmap.meinv5,R.mipmap.meinv6};

    private int hSpace =10;//2个图片间的水平间隔

    private int vSpace =10;//2列图片间的垂直间隔

    private int childWidth,childHeight;

    //记录已添加的图片view
    private ArrayList<View> addedImageViews = new ArrayList<View>();

    private View addIconview;//添加图标view


    public CustomImageGroup(Context context) {
        this(context,null);
    }

    public CustomImageGroup(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomImageGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        addIconview = new View(context);
        addView(addIconview);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);

        //每行显示3个
        childWidth = (widthsize - 2*hSpace) /3;
        childHeight = childWidth;

        Log.i(TAG, "onMeasure: childWidth="+childWidth);


        int childCount = getChildCount();

        for(int i=0; i< childCount;i++ ){
            View childView = getChildAt(i);
            LayoutParams lp = (LayoutParams) childView.getLayoutParams();
            lp.left = (i%3) * (childWidth+hSpace);
            lp.top = (i/3) * (childHeight+vSpace);
        }

        int vwidth = widthsize;
        int vheight = heightsize;
        if( childCount < 3){
            vwidth = childCount*(childWidth+hSpace);
        }
        vheight = ((childCount+3)/3) * (childHeight+vSpace);

        setMeasuredDimension(vwidth,vheight);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        if( addedImageViews.size() == MAX_PHOTO_NUMBER){
            removeView(addIconview);
        }

        int childCount = getChildCount();
        for(int i=0; i< childCount;i++ ){
            View childView = getChildAt(i);
            LayoutParams lp = (LayoutParams) childView.getLayoutParams();
            childView.layout(lp.left,lp.top,lp.left+childWidth,lp.top+childHeight);

            if( addedImageViews.size() < MAX_PHOTO_NUMBER ){
                //已添加的图片数不超过最大图片数时,把最后一个view的图标设置成＋图标
                if( i== childCount-1){
                    childView.setBackgroundResource(R.mipmap.plus);
                    childView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            addPhotoPic();
                        }
                    });

                    break;
                }

            }

            childView.setBackgroundResource(constImageIds[i]);
            childView.setOnClickListener(null);

        }

    }


    private void addPhotoPic() {
        if( addedImageViews.size() <= MAX_PHOTO_NUMBER){
            View picview = new View(getContext());
            addView(picview);
            addedImageViews.add(picview);
            invalidate();
        }
    }


    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new CustomImageGroup.LayoutParams(getContext(),attrs);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new CustomImageGroup.LayoutParams(p);
    }

    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return  new CustomImageGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof CustomImageGroup.LayoutParams;
    }


    public static class  LayoutParams extends ViewGroup.LayoutParams{

        public int left =0;
        public int top =0;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }
    }

}
